﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Controls;
using H2.NET.Business;
using H2.NET.Data.Common;

using System.IO;
using H2.NET.Data.Exceller;


namespace H2.NET.Web.Pages.SPM
{
    public partial class IcdPrice : TaskPage
    {
        #region 属性 Public

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }


        #endregion


        #region 事件 Page_Load(页面加载)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                INFO.Text = string.Format("<br/>表单正在被同事“{0}”抢占处理，拒绝提交！", RobUser);
                INFO.Visible = IsOverFlow;

                //InitDropDownList();

                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                    VD.Checked = true;

                }
                #endregion

                #region 审批环节
                else
                {
                    #region 加载数据并显示
                    HK_IcdPrice listp = (HK_IcdPrice.process == TaskAPI.Process & HK_IcdPrice.incident == TaskAPI.Incident).ListFirst<HK_IcdPrice>();
                    if (listp != null)
                    {
                        ApplicationNo.Text = listp.ApplicationNo;
                        VD.Checked = listp.VD_Checked == "1";
                        SA.Checked = listp.SA_Checked == "1";
                        VD_Category.SelectedValue = listp.VD_Category;
                        Yes_Selected.Checked = listp.Yes_Selcet == "1";
                        No_Selected.Checked = listp.No_Select == "1";
                        Special_Selected.Checked = listp.Special_Select == "1";
                        DrpPriority.SelectedValue = listp.DrpPriority;
                        DMO_DrpPriority.SelectedValue = listp.DMO_DrpPriority;

                        if (!(TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
                        {
                            Condition_Type.Text = listp.Condition_Type;
                            Sorg_DC.Checked = listp.Sorg_DC == "1";
                            Distribution_Channel.Text = listp.Distribution_Channel;
                            Division.Text = listp.Division;
                        }
                        Sales_Organization.SelectedValue = listp.Sales_Organization;
                        Sold_To.Text = listp.Sold_To;

                        if (!(TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
                        {
                            B_Condition_Type.Text = listp.B_Condition_Type;
                            B_Sorg_DC.Checked = listp.B_Sorg_DC == "1";
                            B_Distribution_Channel.Text = listp.B_Distribution_Channel;

                        }


                    }
                    #endregion

                    #region 审批项初始化
                    switch (TaskAPI.Step)
                    {
                        case "Run ICD Price":
                        case "GZ Run Cost":
                        case "DMO Approve":
                        case "DMO Maintain":
                        case "DMO Check":
                            Assessment1.Option.Add(new t_AssessmentOption("Finish", "1", true));
                            Assessment1.Option.Add(new t_AssessmentOption("Return", "2", false));
                            break;
                        case "Finance Manager":
                        case "Manager Approve":
                            Assessment1.Option.Add(new t_AssessmentOption("Approve", "1", true));
                            Assessment1.Option.Add(new t_AssessmentOption("Return", "2", false));
                            break;
                    }
                    #endregion
                }
                #endregion

                #region 设置控件禁启用
                bool flg = (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) ? true : false;

                SetEnable(TaskAPI.IsInitiate && TaskAPI.IsAllowSubmit, VD, SA, VD_Category, DrpPriority,Yes_Selected, No_Selected, Special_Selected, Sales_Organization, Sold_To);
                SetVisible(flg, List_Price_Add, List_Price_Delete, List_Price_Add_B, List_Price_Delete_B);
                SetEnable(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", DMO_DrpPriority);
                SetVisible(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", P_DMO);
                Assessment1.Enable = TaskAPI.IsAllowSubmit;
                SetVisible(TaskAPI.IsAllowSubmit, Send, Split);
                Attachment1.AllowDelete = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                Attachment1.AllowUpload = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;

                if (TaskAPI.IsAllowSubmit)
                {
                    if (TaskAPI.Step == "Run ICD Price" || TaskAPI.Step == "GZ Run Cost")
                    {
                        Attachment1.AllowDelete = true;
                        Attachment1.AllowUpload = true;
                    }
                }
                InitTabs();
                #endregion
            }
        }


        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
            bool flg = TaskAPI.IsAllowSubmit && TaskAPI.IsBeginStep;
            bool flg1 = TaskAPI.IsAllowSubmit && (TaskAPI.IsBeginStep || TaskAPI.Step == "GZ Run Cost" || TaskAPI.Step == "Run ICD Price");
            //bool flg2 = TaskAPI.IsAllowSubmit && TaskAPI.Step == "Run ICD Price";
            //gv_List_Price.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.Label, 0, Align.Left, false, "No", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("00", "Material", BoundColumnType2.TextBox, 60, Align.Left, flg, "Material", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("01", "Description", BoundColumnType2.TextBox, 100, Align.Left, flg, "Description", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("02", "Cost", BoundColumnType2.TextBox, 60, Align.Left, flg1, "Cost", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("03", "Amount", BoundColumnType2.TextBox, 60, Align.Left, flg1, "Amount", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("04", "Unit", BoundColumnType2.DropDownList, 60, Align.Left, false, "Unit", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("05", "Per", BoundColumnType2.TextBox, 60, Align.Left, false, "Per", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("06", "UoM", BoundColumnType2.TextBox, 60, Align.Left, false, "Uom", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("07", "Calculat.type", BoundColumnType2.TextBox, 60, Align.Left, flg, "Calculat_Type", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("08", "ScaleBasis", BoundColumnType2.TextBox, 60, Align.Left, flg, "ScaleBasis", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("09", "Valid From", BoundColumnType2.Calender, 60, Align.Left, flg, "Valid_From", InputType.None, null));
            gv_List_Price.DynamicColumn.Add(new BoundColumn2("10", "Valid To", BoundColumnType2.Calender, 60, Align.Left, flg, "Valid_To", InputType.None, null));


            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("00", "Material", BoundColumnType2.TextBox, 60, Align.Left, flg, "B_Material", InputType.None, null));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("01", "Description", BoundColumnType2.TextBox, 100, Align.Left, flg, "B_Description", InputType.None, null));

            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("02", "Amount", BoundColumnType2.TextBox, 60, Align.Left, flg, "B_Amount", InputType.None, null));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("03", "Unit", BoundColumnType2.DropDownList, 60, Align.Left, flg, "B_Unit", InputType.None, new string[3] { "", "HKD", "CNY" }));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("04", "Per", BoundColumnType2.TextBox, 60, Align.Left, false, "B_Per", InputType.None, null));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("05", "UoM", BoundColumnType2.DropDownList, 60, Align.Left, flg, "B_Uom", InputType.None, new string[3] { "", "FT2", "PC" }));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("06", "Calculat.type", BoundColumnType2.TextBox, 60, Align.Left, flg, "B_Calculat_Type", InputType.None, null));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("07", "ScaleBasis", BoundColumnType2.TextBox, 60, Align.Left, flg, "B_ScaleBasis", InputType.None, null));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("08", "Valid From", BoundColumnType2.Calender, 60, Align.Left, flg, "B_Valid_From", InputType.None, null));
            gv_List_Price_B.DynamicColumn.Add(new BoundColumn2("09", "Valid To", BoundColumnType2.Calender, 60, Align.Left, flg, "B_Valid_To", InputType.None, null));





            if (!Page.IsPostBack)
            {
                gv_List_Price.Key = Key;
                gv_List_Price_B.Key = Key;

                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                    List<HK_IcdPrice_GV> lpm_s = new List<HK_IcdPrice_GV>();
                    for (int i = 0; i < 5; i++)
                    {
                        lpm_s.Add(new HK_IcdPrice_GV());

                    }
                    gv_List_Price.DataSource = lpm_s;

                    List<HK_IcdPrice_B_Gv> lpm2_s = new List<HK_IcdPrice_B_Gv>();
                    for (int i = 0; i < 5; i++)
                    {
                        lpm2_s.Add(new HK_IcdPrice_B_Gv());

                    }
                    gv_List_Price_B.DataSource = lpm2_s;

                }
                #endregion

                #region 审批环节
                else
                {
                    IList<HK_IcdPrice_GV> lpm_s = (HK_IcdPrice_GV.process == TaskAPI.Process & HK_IcdPrice_GV.incident == TaskAPI.Incident).List<HK_IcdPrice_GV>();
                    if (lpm_s.Count == 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            lpm_s.Add(new HK_IcdPrice_GV());

                        }
                    }
                    gv_List_Price.DataSource = lpm_s;

                    IList<HK_IcdPrice_B_Gv> lpm2_s = (HK_IcdPrice_B_Gv.process == TaskAPI.Process & HK_IcdPrice_B_Gv.incident == TaskAPI.Incident).List<HK_IcdPrice_B_Gv>();
                    if (lpm2_s.Count == 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            lpm2_s.Add(new HK_IcdPrice_B_Gv());

                        }
                    }
                    gv_List_Price_B.DataSource = lpm2_s;


                }
                #endregion
            }
            //SetVisible(flg, SEFI_1_Add, SEFI_2_Add);
        }
        #endregion



        #region 事件 Send
        protected void Send_Click(object sender, EventArgs e)
        {
            try
            {
                #region 数据验证
                if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) // 如果是退回的情况，不存在验证
                {
                    List<string> vald_s = new List<string>();
                    vald_s.AddRange(DataValidation());
                    if (vald_s.Count > 0)
                    {
                        Anthem.Manager.AddScriptForClientSideEval(string.Format("ShowMessage(\"{0}\", {1});", CombineMessage(vald_s), vald_s.Count));
                        return;
                    }
                }
                #endregion

                #region 引擎传值
                if (TaskAPI.IsBeginStep)
                {
                    if (TaskAPI.IsInitiate)
                    {
                        ApplicationNo.Text = Util.GenerateSequence(((object[])TaskAPI["PrefixNo"])[0].ToString() + "-" + System.DateTime.Now.ToString("yyMM"), "", 4);
                        TaskAPI.UserName = UserAPI.UserName;
                        TaskAPI["ApplicationNo"] = ApplicationNo.Text;
                        TaskAPI["ApplicatorName"] = UserAPI.UserFullName;
                        TaskAPI["Applicator"] = UserAPI.UserName;
                        TaskAPI["Time"] = System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
                        TaskAPI["ApplicatorEmail"] = UserAPI.Mail;
                        TaskAPI["Department"] = UserAPI.Department;
                        TaskAPI["ServerName"] = Util.AppSettings("ServerName");
                    }


                    TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text)
                                   + " / " + ApplicationNo.Text + (VD.Checked ? " / Pack IP HK " : "") + (SA.Checked ? " / Wall Paper " : "");


                    //TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text) + " / " + ApplicationNo.Text;

                    if (Yes_Selected.Checked)
                    {
                        TaskAPI["Finance"] = "1";
                    }
                    if (No_Selected.Checked)
                    {
                        TaskAPI["Finance"] = "0";
                    }
                    if (Special_Selected.Checked)
                    {
                        TaskAPI["Finance"] = "2";
                    }
                    TaskAPI["Actions"] = VD_Category.SelectedValue;
                    TaskAPI["Remark_by_Initiator"] = Assessment1.Assessment.Remark;
                    TaskAPI["DrpPriority"] = DrpPriority.SelectedItem.Text;

                    #region 从数据库取得流程步骤处理人
                    SortedList<string, string> rec_a = new SortedList<string, string>();

                    IList<mdc_Recipient> rec_s = (mdc_Recipient.process == TaskAPI.Process & (mdc_Recipient.distribute == VD_Category.SelectedValue | mdc_Recipient.distribute == "*") & mdc_Recipient.status == 1).List<mdc_Recipient>();
                    foreach (mdc_Recipient rec in rec_s)
                    {
                        if (!rec_a.ContainsKey(rec.Step))
                        {
                            rec_a.Add(rec.Step, rec.Recipient);
                            continue;
                        }

                        if (rec_a.Values[rec_a.IndexOfKey(rec.Step)].Contains(rec.Recipient))
                        {
                            continue;
                        }

                        rec_a[rec.Step] = rec_a.Values[rec_a.IndexOfKey(rec.Step)] + "," + rec.Recipient;
                    }

                    //步骤处理人赋值
                    for (int i = 0; i < rec_a.Keys.Count; i++)
                    {
                        TaskAPI["R_" + rec_a.Keys[i].Replace(" ", "_")] = rec_a.Values[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    }

                    m_OrgChart oc = (m_OrgChart.empID.In(m_OrgChart.supervisorEmpID, m_OrgChart.winID == UserAPI.UserName)).ListFirst<m_OrgChart>();
                    if (oc != null)
                    {
                        TaskAPI["R_Manager_Approve"] = oc.WinID;
                    }


                    #endregion
                }
                else
                {
                    TaskAPI["ApproveResult"] = Assessment1.Assessment.Opinion;
                    TaskAPI["TaskID"] = TaskAPI.TaskID;
                }

                #endregion

                #region 提交步骤
                bool flg = false;
                Storage.TransactionExecute(o =>
                {
                    #region “Fill in the form”步骤
                    if (TaskAPI.IsBeginStep)
                    {
                        HK_IcdPrice listp = new HK_IcdPrice();
                        if ((HK_IcdPrice.process == TaskAPI.Process & HK_IcdPrice.incident == TaskAPI.Incident & HK_IcdPrice.userName == UserAPI.UserName).Count<HK_IcdPrice>() > 0)
                        {
                            listp = (HK_IcdPrice.process == TaskAPI.Process & HK_IcdPrice.incident == TaskAPI.Incident & HK_IcdPrice.userName == UserAPI.UserName).ListFirst<HK_IcdPrice>();
                        }
                        else
                        {
                            listp.Process = TaskAPI.Process;
                            listp.Incident = TaskAPI.Incident;
                            listp.UserName = UserAPI.UserName;
                            listp.UserFullName = UserAPI.UserFullName;
                            listp.ApplicationNo = ApplicationNo.Text;
                            listp.Time = System.DateTime.Now;
                            listp.Status = 0;
                            listp.Department = UserAPI.Department;
                        }

                        listp.VD_Category = VD_Category.SelectedValue;
                        listp.DrpPriority = DrpPriority.SelectedValue;
                        listp.VD_Checked = VD.Checked ? "1" : "0";
                        listp.SA_Checked = SA.Checked ? "1" : "0";
                        //hk_ListPrice.Actions = VD_Category.SelectedItem.Text;

                        listp.Yes_Selcet = Yes_Selected.Checked ? "1" : "0";
                        listp.No_Select = No_Selected.Checked ? "1" : "0";
                        listp.Special_Select = Special_Selected.Checked ? "1" : "0";

                        #region VD
                        if (VD.Checked)
                        {

                            listp.Condition_Type = Condition_Type.Text;
                            listp.Sorg_DC = Sorg_DC.Checked ? "1" : "0";
                            listp.Sales_Organization = Sales_Organization.SelectedValue;
                            listp.Distribution_Channel = Distribution_Channel.Text;
                            listp.Division = Division.Text;
                            listp.Sold_To = Sold_To.Text;
                        }
                        #endregion

                        #region SA
                        if (SA.Checked)
                        {
                            listp.B_Condition_Type = B_Condition_Type.Text;
                            listp.B_Sorg_DC = B_Sorg_DC.Checked ? "1" : "0";
                            listp.B_Distribution_Channel = B_Distribution_Channel.Text;
                        }
                        #endregion

                        (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });

                        Storage.Save(Storage.Context1, listp);
                    }
                    #endregion

                    Save();

                    #region Priority功能
                    if (TaskAPI.IsBeginStep && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        mdc_Priority pro;
                        if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                        {
                            pro = new mdc_Priority();
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }
                        else
                        {
                            pro = new mdc_Priority();
                            pro.Process = TaskAPI.Process;
                            pro.Incident = TaskAPI.Incident;
                            pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }

                    }


                    if ((TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check") && !string.IsNullOrEmpty(this.DMO_DrpPriority.SelectedValue) && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {

                        //mdc_Priority pro;
                        //pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                        //pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                        //Storage.Save(pro);
                        mdc_Priority pro;
                        if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                        {
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }
                        else
                        {
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }


                    }
                    #endregion

                    #region “DMO Maintain”步骤
                    if (TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check")
                    {

                        TaskAPI["DMO_DrpPriority"] = DMO_DrpPriority.SelectedItem.Text;
                        (HK_IcdPrice.process == TaskAPI.Process & HK_IcdPrice.incident == TaskAPI.Incident).Edit<HK_IcdPrice>(p =>
                        {
                            // p.SAP_Code = SAP_Code.Text;
                            p.DMO_DrpPriority = DMO_DrpPriority.SelectedValue;

                        }
                        );
                    }
                    #endregion

                    Storage.Save(Assessment1.Assessment);    //审批意见

                    //多节点退回
                    if (!string.IsNullOrEmpty(Assessment1.JumpStep))
                    {
                        Assessment1.SetAsReturn(true);
                        TaskAPI.DirectActive(Assessment1.JumpStep);
                    }
                    else
                    {
                        Assessment1.SetAsReturn(false);
                    }
                    Assessment1.SubmitTo();

                    flg = TaskAPI.Send();
                });

                //如果数据库写入成功且流程发起成功的情况，将业务表数据与流程实例号进行关联
                if (TaskAPI.IsInitiate)
                {
                    if (!string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == 0).Edit<mdc_Priority>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });
                    }
                    (HK_IcdPrice.process == TaskAPI.Process & HK_IcdPrice.incident == 0 & HK_IcdPrice.userName == UserAPI.UserName).Edit<HK_IcdPrice>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == 0 & t_Assessment.userName == UserAPI.UserName).Edit<t_Assessment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });


                    (HK_IcdPrice_GV.process == TaskAPI.Process & HK_IcdPrice_GV.incident == 0 & HK_IcdPrice_GV.userName == UserAPI.UserName).Edit<HK_IcdPrice_GV>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (HK_IcdPrice_B_Gv.process == TaskAPI.Process & HK_IcdPrice_B_Gv.incident == 0 & HK_IcdPrice_B_Gv.userName == UserAPI.UserName).Edit<HK_IcdPrice_B_Gv>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });


                }
                #endregion

                Send.Visible = false;
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Success + "',true)");
            }
            catch (Exception ex)
            {
                TaskDB.Log("", false, "调用NVA方法Send_Click出错：" + ex.ToString());
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Failed + "',false)");
            }
        }
        #endregion


        #region 事件 CheckedChanged
        protected void VD_CheckedChanged(object sender, EventArgs e)
        {

            if (!VD.Checked && !SA.Checked)
            {
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留一个选择项！',false)");
                VD.Checked = true;
            }
            SetVisible(false, P_VD, P_SA);

            switch (((Anthem.CheckBox)sender).ID)
            {
                case "VD":

                    if (VD.Checked)
                    {
                        // SA.Checked = false;
                        //SA2.Checked = false;
                        //SA3.Checked = false;
                    }
                    //VD.Checked = !SA.Checked;
                    //VD.Checked = !SA2.Checked;
                    //VD.Checked = !SA3.Checked;
                    VD_L.Visible = VD.Checked;
                    break;

                case "SA":
                    if (SA.Checked)
                    {
                        //VD.Checked = false;
                        //SA2.Checked = false;
                        //SA3.Checked = false;
                    }
                    SA_L.Visible = SA.Checked;
                    //if (!SA.Checked & !SA2.Checked) SA.Checked = true;
                    //SA.Checked = !VD.Checked;
                    //SA.Checked = !SA2.Checked;
                    //SA.Checked = !SA3.Checked;

                    //if (!SA.Checked & SA2.Checked) { SA2_L.CssClass = "tabs1"; Tabs_Click(SA2_L, e); }
                    break;

                //case "SA2":

                //    if (SA2.Checked)
                //    {
                //SA.Checked = false;
                //VD.Checked = false;
                //SA3.Checked = false;
                //}
                //SA2.Checked = !SA.Checked;
                //SA2.Checked = !VD.Checked;
                //SA2.Checked = !SA3.Checked;
                //SA2_L.Visible = SA2.Checked;
                //break;



            }


            InitTabs();
        }
        #endregion

        #region 事件 Category变换
        protected void VD_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetEnable((VD_Category.SelectedValue == "1" || VD_Category.SelectedValue == "2" || VD_Category.SelectedValue == "3"), Sales_Organization, Sold_To, DrpPriority);

        }
        #endregion

        #region 事件 Tabs切换
        protected void Tabs_Click(object sender, EventArgs e)
        {
            if (((LinkButton)sender).CssClass == "tabs2")
            {
                return;
            }

            #region 初始化
            SetVisible(false, P_VD, P_SA);
            VD_L.CssClass = "tabs1";
            SA_L.CssClass = "tabs1";
            // SA2_L.CssClass = "tabs1";
            #endregion

            #region Tabs切换动作
            switch (((LinkButton)sender).ID)
            {
                case "VD_L":
                    VD_L.CssClass = "tabs2";
                    P_VD.Visible = true;
                    break;
                case "SA_L":
                    SA_L.CssClass = "tabs2";
                    P_SA.Visible = true;
                    break;
                //case "SA2_L":
                //    SA2_L.CssClass = "tabs2";
                //    P_SA2.Visible = true;
                //    break;

            }
            #endregion
        }
        #endregion

        #region 方法 初始化Tabs
        protected void InitTabs()
        {
            VD_L.Visible = VD.Checked;
            SA_L.Visible = SA.Checked;
            // SA2_L.Visible = SA2.Checked;

            SetVisible(false, P_VD, P_SA);
            VD_L.CssClass = "tabs1";
            SA_L.CssClass = "tabs1";
            // SA2_L.CssClass = "tabs1";


            //默认第1个显示 Tabs被选中
            if (VD_L.Visible)
            {
                VD_L.CssClass = "tabs2";
                P_VD.Visible = true;
                return;
            }

            if (SA_L.Visible)
            {
                SA_L.CssClass = "tabs2";
                P_SA.Visible = true;
                return;
            }

            //if (SA2_L.Visible)
            //{
            //    SA2_L.CssClass = "tabs2";
            //    P_SA2.Visible = true;
            //    return;
            //}



        }
        #endregion

        #region 方法 InitDropdownList
        private void InitDropDownList()
        {
            if (!IsLoaded)
            {

                IsLoaded = true;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            //if (!Status.IsEdited)
            //{
            //    return;
            //}
            //将数据库里当前流程对应的 mdc_Ins_Plan_1 状态置为逻辑删除
            (HK_IcdPrice_GV.process == TaskAPI.Process & HK_IcdPrice_GV.incident == TaskAPI.Incident).Edit<HK_IcdPrice_GV>(p =>
            {
                p.Status = 1;
            }
            );
            foreach (HK_IcdPrice_GV lpm_1 in (List<HK_IcdPrice_GV>)gv_List_Price.DataSource)
            {
                if (string.IsNullOrEmpty(lpm_1.Material) && string.IsNullOrEmpty(lpm_1.Description) && string.IsNullOrEmpty(lpm_1.Calculat_Type) && string.IsNullOrEmpty(lpm_1.ScaleBasis))
                {
                    continue;
                }
                lpm_1.Process = TaskAPI.Process;
                lpm_1.Incident = TaskAPI.Incident;
                lpm_1.UserName = UserAPI.UserName;
                lpm_1.Time = System.DateTime.Now;
                lpm_1.Status = 0;
                Storage.Save(lpm_1);
            }

            (HK_IcdPrice_B_Gv.process == TaskAPI.Process & HK_IcdPrice_B_Gv.incident == TaskAPI.Incident).Edit<HK_IcdPrice_B_Gv>(p =>
            {
                p.Status = 1;
            }
            );
            foreach (HK_IcdPrice_B_Gv lpm_2 in (List<HK_IcdPrice_B_Gv>)gv_List_Price_B.DataSource)
            {
                if (string.IsNullOrEmpty(lpm_2.B_Material) && string.IsNullOrEmpty(lpm_2.B_Description) && string.IsNullOrEmpty(lpm_2.B_Calculat_Type) && string.IsNullOrEmpty(lpm_2.B_ScaleBasis))
                {
                    continue;
                }
                lpm_2.Process = TaskAPI.Process;
                lpm_2.Incident = TaskAPI.Incident;
                lpm_2.UserName = UserAPI.UserName;
                lpm_2.Time = System.DateTime.Now;
                lpm_2.Status = 0;
                Storage.Save(lpm_2);
            }

        }

        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {


            switch (((Anthem.Button)sender).ID)
            {
                case "List_Price_Add":
                    List<HK_IcdPrice_GV> list_1_s = (List<HK_IcdPrice_GV>)gv_List_Price.DataSource;
                    HK_IcdPrice_GV list_1 = new HK_IcdPrice_GV();
                    list_1_s.Insert(list_1_s.Count, list_1);
                    gv_List_Price.DataSource = list_1_s;
                    gv_List_Price.DataBind();
                    break;

                case "List_Price_Add_B":
                    List<HK_IcdPrice_B_Gv> list_2_s = (List<HK_IcdPrice_B_Gv>)gv_List_Price_B.DataSource;
                    HK_IcdPrice_B_Gv list_2 = new HK_IcdPrice_B_Gv();
                    list_2_s.Insert(list_2_s.Count, list_2);
                    gv_List_Price_B.DataSource = list_2_s;
                    gv_List_Price_B.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {


            switch (((Anthem.Button)sender).ID)
            {
                case "List_Price_Delete":
                    List<HK_IcdPrice_GV> list_1_s = (List<HK_IcdPrice_GV>)gv_List_Price.DataSource;
                    IList<HK_IcdPrice_GV> list = (HK_IcdPrice_GV.process == TaskAPI.Process & HK_IcdPrice_GV.incident == TaskAPI.Incident).List<HK_IcdPrice_GV>();
                    if (list_1_s.Count == 1)
                    {

                        return;
                    }
                    list_1_s.RemoveAt(gv_List_Price.ChangedRowIndex == -1 || gv_List_Price.ChangedRowIndex >= list_1_s.Count ? list_1_s.Count - 1 : gv_List_Price.ChangedRowIndex);
                    bool flag = false;
                    foreach (HK_IcdPrice_GV list_1 in list)
                    {

                        foreach (HK_IcdPrice_GV list_2 in list_1_s)
                        {
                            if (list_2.ID == list_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(list_1);
                        }
                        flag = false;
                    }
                    gv_List_Price.DataSource = list_1_s;
                    gv_List_Price.DataBind();
                    break;

                case "List_Price_Delete_B":
                    List<HK_IcdPrice_B_Gv> special_2_s = (List<HK_IcdPrice_B_Gv>)gv_List_Price_B.DataSource;
                    IList<HK_IcdPrice_B_Gv> special2 = (HK_IcdPrice_B_Gv.process == TaskAPI.Process & HK_IcdPrice_B_Gv.incident == TaskAPI.Incident).List<HK_IcdPrice_B_Gv>();
                    if (special_2_s.Count == 1)
                    {

                        return;
                    }
                    special_2_s.RemoveAt(gv_List_Price_B.ChangedRowIndex == -1 || gv_List_Price_B.ChangedRowIndex >= special_2_s.Count ? special_2_s.Count - 1 : gv_List_Price_B.ChangedRowIndex);
                    bool flag1 = false;
                    foreach (HK_IcdPrice_B_Gv special_2 in special2)
                    {

                        foreach (HK_IcdPrice_B_Gv special_22 in special_2_s)
                        {
                            if (special_22.ID == special_2.ID)
                            {
                                flag1 = true;
                                break;
                            }
                        }
                        if (!flag1)
                        {
                            Storage.Delete(special_2);
                        }
                        flag1 = false;
                    }
                    gv_List_Price_B.DataSource = special_2_s;
                    gv_List_Price_B.DataBind();
                    break;
            }

        }
        #endregion


        #region 方法 数据验证
        private List<string> DataValidation()
        {




            List<string> vald_s = new List<string>();
            List<string> vald_s_2 = new List<string>();
            List<Regular> regs = new List<Regular>();

            if (!Yes_Selected.Checked && !No_Selected.Checked && !Special_Selected.Checked)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, "", "是否是财务人员或者特殊类型，必须选择其一"));
            }



            #region VD
            if (VD.Checked)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, Sales_Organization.SelectedValue, " Sales Organization 必填"));
                regs.Add(new Regular(RegularExp.NotEmpty, Sold_To.Text, " Customer(sold to) 必填"));
                int i = 1;
                foreach (HK_IcdPrice_GV LPM in (List<HK_IcdPrice_GV>)gv_List_Price.DataSource)
                {
                    if (string.IsNullOrEmpty(LPM.Material) && string.IsNullOrEmpty(LPM.Amount) && string.IsNullOrEmpty(LPM.Valid_From) && string.IsNullOrEmpty(LPM.Valid_To))
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(LPM.Material))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPM.Material, "&nbsp;&nbsp;行" + i + ":Material 为必填 "));
                    }
                    if (string.IsNullOrEmpty(LPM.Unit))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPM.Unit, "&nbsp;&nbsp;行" + i + ":Unit 为必填 "));
                    }

                    if (string.IsNullOrEmpty(LPM.Amount))
                    {
                        //regs.Add(new Regular(RegularExp.NotEmpty, LPM.Amount, "&nbsp;&nbsp;行" + i + ":Amount 为必填 "));
                    }


                    if (string.IsNullOrEmpty(LPM.Valid_From))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPM.Valid_From, "&nbsp;&nbsp;行" + i + ":Valid From 为必填 "));
                    }

                    if (string.IsNullOrEmpty(LPM.Valid_To))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPM.Valid_To, "&nbsp;&nbsp;行" + i + ":Valid To 为必填 "));
                    }
                    i++;
                }


            }
            vald_s = Validation(regs);
            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>Pack IP HK</b></font>");
            }

            #endregion

            #region SA
            regs.Clear();
            if (SA.Checked)
            {
                int i = 1;
                foreach (HK_IcdPrice_B_Gv LPB in (List<HK_IcdPrice_B_Gv>)gv_List_Price_B.DataSource)
                {
                    if (string.IsNullOrEmpty(LPB.B_Material) && string.IsNullOrEmpty(LPB.B_Amount) && string.IsNullOrEmpty(LPB.B_Uom) && string.IsNullOrEmpty(LPB.B_Unit) && string.IsNullOrEmpty(LPB.B_Valid_From) && string.IsNullOrEmpty(LPB.B_Valid_To))
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(LPB.B_Material))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPB.B_Material, "&nbsp;&nbsp;行" + i + ":Material 为必填 "));
                    }
                    if (string.IsNullOrEmpty(LPB.B_Unit))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPB.B_Unit, "&nbsp;&nbsp;行" + i + ":Unit 为必填 "));
                    }

                    if (string.IsNullOrEmpty(LPB.B_Amount))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPB.B_Amount, "&nbsp;&nbsp;行" + i + ":Amount 为必填 "));
                    }
                    if (string.IsNullOrEmpty(LPB.B_Uom))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPB.B_Uom, "&nbsp;&nbsp;行" + i + ":Uom 为必填 "));
                    }

                    if (string.IsNullOrEmpty(LPB.B_Valid_From))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPB.B_Valid_From, "&nbsp;&nbsp;行" + i + ":Valid From 为必填 "));
                    }

                    if (string.IsNullOrEmpty(LPB.B_Valid_To))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, LPB.B_Valid_To, "&nbsp;&nbsp;行" + i + ":Valid To 为必填 "));
                    }
                    i++;
                }

            }
            vald_s_2 = Validation(regs);
            if (vald_s_2.Count > 0)
            {
                vald_s_2.Insert(0, "<font color='#0000FF'><b>Wall Paper</b></font>");
                vald_s.AddRange(vald_s_2);
            }
            #endregion


            return vald_s;
        }
        #endregion
        #region 选择是否财务人员
        protected void Yes_Selected_CheckedChanged(object sender, EventArgs e)
        {
            switch (((Anthem.CheckBox)sender).ID)
            {
                case "Yes_Selected":

                    No_Selected.Checked = false;
                    Special_Selected.Checked = false;
                    break;

                case "No_Selected":
                    Yes_Selected.Checked = false;
                    Special_Selected.Checked = false;
                    break;

                case "Special_Selected":
                    Yes_Selected.Checked = false;
                    No_Selected.Checked = false;
                    break;
            }
        }
        #endregion
        #region 方法 Others
        protected void VD_Payment_Methods_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private string getFormCheckList(Anthem.CheckBoxList ck)
        {
            string t = "";
            for (int i = 0; i < ck.Items.Count; i++)
                if (ck.Items[i].Selected)
                    t += "," + ck.Items[i].Value;
                else
                    t += ",";
            return t;
        }
        private Anthem.CheckBoxList setCheckList(string t, Anthem.CheckBoxList ck)
        {
            try
            {
                string[] tt = t.Split(',');

                for (int i = 0; i <= tt.GetUpperBound(0); i++)
                {
                    for (int j = 0; j < ck.Items.Count; j++)
                    {
                        if (ck.Items[j].Value == tt[i].ToString())
                        { ck.Items[j].Selected = true; break; }
                    }
                }
            }
            catch { }
            return ck;
        }
        #endregion
    }
}
